/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.controller;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.Local;
import edu.upc.clase.demo.service.LocalService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Jose Vasquez
 */
@Controller("localController")
public class LocalController {
  
    @Autowired
    private LocalService localService;
    private static Logger log = LoggerFactory.getLogger(LocalController.class);
   
    @RequestMapping("/local/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("local/index");
        List<Local> local = localService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("local", local);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/local/new", method = RequestMethod.GET)
    public ModelAndView newLocal() {
        ModelAndView mav = new ModelAndView("local/new");
        Local local = new Local();
        mav.getModelMap().put("local", local);
        return mav;
    }

    @RequestMapping(value = "/local/new", method = RequestMethod.POST)
    public String createLocal(@ModelAttribute("local")Local local, SessionStatus status) {    
        localService.insertar(local);
        status.setComplete();
        return "redirect:/pages/local/index";
        
    }
    
    @RequestMapping(value = "/local/edit", method = RequestMethod.GET)
    public ModelAndView editLocal(@RequestParam("CoLocal")Integer CoLocal) {    
        ModelAndView mav = new ModelAndView("local/edit");
        Local local = localService.buscar(CoLocal);
        mav.getModelMap().put("local", local);
        return mav;        
    }
    
    @RequestMapping(value="/local/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("local") Local local, SessionStatus status) {
        localService.actualizar(local);
        status.setComplete();
        return "redirect:/pages/local/index";
    }
    
     
    @RequestMapping("/local/delete")
    public ModelAndView delete(@RequestParam("CoLocal")Integer CoLocal)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/local/index");
        Local local = localService.buscar(CoLocal);
        localService.eliminar(local);
        return mav;
    }
    
    @RequestMapping(value = "/local/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("local/resultadoBusqueda");
        log.info("resultado = " + localService.buscarPorNombre(criterio.getNoLocal()).size());
        mav.getModel().put("resultado",localService.buscarPorNombre(criterio.getNoLocal()));
        return mav;
    }
    
}
